home *** CD-ROM | disk | FTP | other *** search
Wrap
/******************************************************************/ /* */ /* TurboCAD for Windows */ /* Copyright (c) 1993 - 2001 */ /* International Microcomputer Software, Inc. */ /* (IMSI) */ /* All rights reserved. */ /* */ /******************************************************************/ // SdiDialog.cpp : implementation file // #include "stdafx.h" #include "InsSmObj.h" #include "SdiDialog.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //const HRESULT hRes = S_OK; const IID LIBID_IMSIGXLib = {0x6A481400,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}}; const IID IID_PickResult = {0x6A481123,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}}; const IID IID_Graphics = {0x6A48110A, 0xE531, 0x11CF, {0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}}; const IID IID_View = {0x6A481110, 0xE531, 0x11CF, {0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}}; const IID IID_Window = {0x6A481124, 0xE531, 0x11CF, {0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF} }; const CLSID CLSID_Application = {0x6A481001,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}}; const CLSID CLSID_XGraphic = {0x6A481803,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}}; static const IID IID_IAppEvents = { 0x6A481301, 0xE531, 0x11CF, { 0xA1, 0x15, 0x0, 0xA0, 0x24, 0x15, 0x8D, 0xAF } }; ///////////////////////////////////////////////////////////////////////////// // CSdiDialog dialog long a = 0; BEGIN_MESSAGE_MAP(CSdiDialog, CDialog) //{{AFX_MSG_MAP(CSdiDialog) ON_BN_CLICKED(ID_CLOSE, OnClose) ON_WM_SYSCOMMAND() ON_WM_DESTROY() ON_BN_CLICKED(ID_INSERTSYMB, OnInsertsymb) //}}AFX_MSG_MAP /// ON_LBN_DBLCLK(IDC_LIST1, onDoubleClick) END_MESSAGE_MAP() BEGIN_DISPATCH_MAP(CSdiDialog, CDialog) //{{AFX_DISPATCH_MAP(CTCEventsApp) DISP_FUNCTION(CSdiDialog, "MouseDown", MouseDown, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_I2 VTS_I4 VTS_I4 VTS_I4 VTS_PBOOL) DISP_FUNCTION(CSdiDialog, "MouseUp", MouseUp, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_I2 VTS_I4 VTS_I4 VTS_I4 VTS_PBOOL) DISP_FUNCTION(CSdiDialog, "MouseMove", MouseMove, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_I4 VTS_I4 VTS_I4 VTS_PBOOL) DISP_FUNCTION(CSdiDialog, "DrawingBeforeClose", DrawingBeforeClose, VT_EMPTY, VTS_DISPATCH VTS_PBOOL) //}}AFX_DISPATCH_MAP END_DISPATCH_MAP() BEGIN_INTERFACE_MAP(CSdiDialog, CDialog) INTERFACE_PART(CSdiDialog, IID_IAppEvents, Dispatch) END_INTERFACE_MAP() void CSdiDialog::MouseDown(LPDISPATCH WhichDrawing, LPDISPATCH WhichView, LPDISPATCH WhichWindow, short Button, long Shift, long X, long Y, IMSI_BOOL* pbCancel) { // Do nothing on this event *pbCancel = FALSE; } void CSdiDialog::MouseUp(LPDISPATCH WhichDrawing, LPDISPATCH WhichView, LPDISPATCH WhichWindow, short Button, long Shift, long X, long Y, IMSI_BOOL* pbCancel) { COleVariant varItem = (0L); COleVariant varOptional(varMissing); IDrawing *pIDwgTrg = NULL; /// current drawing pIDwgTrg = (IDrawing*) WhichDrawing; pIDwgTrg->AddRef(); Graphics *pGrs = NULL; *pbCancel = FALSE; CPoint point; point.x = X; point.y = Y; try { if(m_pDragGraphic != 0) { hRes = pIDwgTrg->get_Graphics(&pGrs); // get current drawing's graphics collection CHECK_HRESULT(hRes) CPreviewWnd *pPreview = (CPreviewWnd *) (GetDlgItem(IDC_PREVIEW)); hRes = pGrs->AddGraphic(m_pDragGraphic, varOptional, varOptional); CHECK_HRESULT(hRes) hRes = m_pDragGraphic->Update(); CHECK_HRESULT(hRes) if(m_pTCADView != NULL) { m_pTCADView->Release(); m_pTCADView = NULL; } m_pTCADView = (View*) WhichView; m_pTCADView->AddRef(); // End drag of external object EndDrag(point); hRes = m_pTCADView->Refresh(); CHECK_HRESULT(hRes) m_DraggingMode = FALSE; } } catch (...) { TRACE_EXCEPTION("CSdiDialog::MouseUp") } if(m_pDragGraphic != NULL) { m_pDragGraphic->Release(); m_pDragGraphic = NULL; } if (m_pTCADView != NULL) { m_pTCADView->Release(); m_pTCADView = NULL; } if (pGrs != NULL) { pGrs->Release(); pGrs = NULL; } if(pIDwgTrg != NULL) { pIDwgTrg->Release(); pIDwgTrg = NULL; } } void CSdiDialog::MouseMove(LPDISPATCH WhichDrawing, LPDISPATCH WhichView, LPDISPATCH WhichWindow, long Shift, long X, long Y, IMSI_BOOL* pbCancel) { CPoint point; point.x = X; point.y = Y; *pbCancel = FALSE; View *pTCADCurrentView = NULL; COleVariant varItem = (1L); try { if (m_DraggingMode == TRUE) // Run BeginDrag function on first MouseMove after user start drag ExternalObject { HCURSOR hOC = ::SetCursor(::LoadCursor(NULL,IDC_WAIT)); if(m_pTCADView != NULL) { RELEASE(m_pTCADView); } m_pTCADView = (View *)WhichView; m_pTCADView->AddRef(); CPreviewWnd *pPreview = (CPreviewWnd *) (GetDlgItem(IDC_PREVIEW)); hRes = pPreview->m_pPreviewGraphic->Duplicate(&m_pDragGraphic); CHECK_HRESULT(hRes) m_pDragGraphic->Release(); m_pDragGraphic = NULL; hRes = pPreview->m_pPreviewGrs->Remove(&varItem, &m_pDragGraphic); CHECK_HRESULT(hRes) hRes = m_pDragGraphic->put_ID(0); CHECK_HRESULT(hRes) m_DraggingMode = FALSE; ::SetCursor(hOC); BeginDrag(point); } else { if (m_pDragGraphic != NULL && !m_bDragging) { if (!BeginDrag(point)) { m_pDragGraphic->Release(); m_pDragGraphic = NULL; } } else if (m_bDragging) { pTCADCurrentView = (View *)WhichView; pTCADCurrentView->AddRef(); //It is possible in TurboCAD to have several views opened in the same time //(ISOMETRIC and PLAN for example) so we have to free pointer for m_pTCADView and get it again if (pTCADCurrentView != m_pTCADView) // if view is changed (user drag external object over another view) then restart drag { if(m_pTCADView != NULL) { RELEASE(m_pTCADView); } m_pTCADView = (View *)WhichView; m_pTCADView->AddRef(); pTCADCurrentView->Release(); pTCADCurrentView = NULL; RestartDrag(point); } Drag(point); } } } catch (...) { TRACE_EXCEPTION("CSdiDialog::OnMouseMove") } } void CSdiDialog::DisconnectEvents() { if (m_pIApplication == NULL || m_dwEventConnection == 0) return; try { HRESULT hRes = m_pIApplication->DisconnectEvents(m_dwEventConnection); CHECK_HRESULT(hRes) if (FAILED(hRes)) { AfxMessageBox(IDS_DISCONNECTFAILED); return; } m_dwEventConnection = 0; } catch (...) { TRACE_EXCEPTION("CSdiDialog::DisconnectEvents") } if(m_pIApplication != NULL) { m_pIApplication->Release(); m_pIApplication = NULL; } } CSdiDialog::CSdiDialog(CInsSmObj* pTool, CWnd* pParent, /*=NULL*/RegenMethods *pRgs) : CDialog(CSdiDialog::IDD, pParent), m_dwEventConnection(0), // m_dwEventMask(~0UL), m_pIApplication(NULL), m_ptMouseDown(CPoint(0, 0)), m_DraggingMode(FALSE), m_bDragging(FALSE), m_pTCADView(NULL), m_pDragGraphic(NULL) { //{{AFX_DATA_INIT(CSdiDialog) m_listbox = _T(""); //}}AFX_DATA_INIT m_dwEventMask = imsiEventMouseUp // | imsiEventSelectionChange | imsiEventMouseDown | imsiEventMouseMove | imsiEventMouseDown | imsiEventDrawingBeforeClose; m_pTool = pTool; m_pTool->AddRef(); m_pTool->m_bRunned = TRUE; m_pRegMets = pRgs; ADDREF(m_pRegMets); } void CSdiDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSdiDialog) DDX_LBString(pDX, IDC_EXTOBJECTSLIST, m_listbox); //}}AFX_DATA_MAP } ///////////////////////////////////////////////////////////////////////////// // CSdiDialog message handlers BOOL CSdiDialog::OnInitDialog() { CDialog::OnInitDialog(); m_list.SubclassDlgItem( IDC_EXTOBJECTSLIST , this ); CExternalObjectsList *pList = (CExternalObjectsList *)GetDlgItem(IDC_EXTOBJECTSLIST); m_wndView.SubclassDlgItem(IDC_PREVIEW, this); CPreviewWnd *pView = (CPreviewWnd *)GetDlgItem(IDC_PREVIEW); COleVariant varItem; CString cstrDesc; CString cstrName; HRESULT hRes = S_OK; RegenMethod *pRegMet = NULL; BSTR bstrDescription = NULL; BSTR bstrName = NULL; ImsiRegenMethodType regenType; EnableAutomation(); long NumRegs = 0; try { hRes = m_pRegMets->get_Count(&NumRegs); CHECK_HRESULT(hRes) // fill the ExternalObject list with list of external objects for(long i = 0;i < NumRegs; i++) { varItem = i; // Retreive collection of External objects (Regen Methods - in TurboCAD's terminology) hRes = m_pRegMets->get_Item(&varItem,&pRegMet); CHECK_HRESULT(hRes) hRes = pRegMet->get_Type(®enType); CHECK_HRESULT(hRes) // Check if the External object is created with SDK and support automation. if(regenType == imsiAutomation) { hRes = pRegMet->get_Description(&bstrDescription); if (SUCCEEDED(hRes)) { cstrDesc = bstrDescription; ::SysFreeString(bstrDescription); bstrDescription = NULL; hRes = pRegMet->get_Name(&bstrName); CHECK_HRESULT(hRes) cstrName = bstrName; ::SysFreeString(bstrName); bstrName = NULL; // cut some external objects from the list because they are auxiliary objects. if(cstrName == "Foundation.House" || cstrName == "AutoDim.House" || cstrName == "Schedule.Block" || cstrName == "Fillet3D_Regen.Fillet3D") { //Do nothing } else { pList->AddString(cstrName); } } } } if(pList->SetCurSel(0) != LB_ERR) { pList->GetText(0 ,(pList->m_cstrCurSelected)); pView->CreatePreview(); pView->DoPreview(); // Connect to TurboCAD's events ConnectEvents(); } } catch (...) { TRACE_EXCEPTION("CSdiDialog::OnInitDialog") } if (pRegMet != NULL) { pRegMet->Release(); pRegMet = NULL; } if (bstrDescription != NULL) ::SysFreeString(bstrDescription); if (bstrName != NULL) ::SysFreeString(bstrName); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CSdiDialog::PostNcDestroy() { CDialog::PostNcDestroy(); RELEASE(m_pRegMets) delete this; } void CSdiDialog::OnClose() { // TODO: Add your message handler code here and/or call default if(m_dwEventConnection != 0) // Disconnect from TurboCAD's events if connection is established. { DisconnectEvents(); } m_pTool->m_bRunned = FALSE; if(m_pTool != NULL) { m_pTool->Release (); m_pTool = NULL; } if(m_pRegMets != NULL) { m_pRegMets->Release(); m_pRegMets = NULL; } CPreviewWnd *pView = (CPreviewWnd *)GetDlgItem(IDC_PREVIEW); pView->ClearAll(); CDialog::OnClose(); DestroyWindow(); } void CSdiDialog::ConnectEvents() { HRESULT hRes = S_OK; if (m_dwEventConnection != 0) { AfxMessageBox(IDS_ALREADYCONNECTED); return; } m_pIApplication = NULL; try { hRes = m_pRegMets->get_Application(&m_pIApplication); CHECK_HRESULT(hRes) if (m_pIApplication == NULL) { AfxMessageBox(IDS_CANTGETTCAPP); return; } IDispatch* pUnkEventSink = GetIDispatch(TRUE); UpdateData(TRUE); COleVariant var((long)m_dwEventMask); hRes = m_pIApplication->ConnectEvents(pUnkEventSink, &var, (long*)&m_dwEventConnection); CHECK_HRESULT(hRes) if (FAILED(hRes)) { AfxMessageBox(IDS_CONNECTIONNOTESTABLISHED); return; } } catch (...) { TRACE_EXCEPTION("CSdiDialog::ConnectEvents") } } void CSdiDialog::OnSysCommand(UINT nID, LPARAM lParam ) { if (nID == SC_CLOSE) { if(m_dwEventConnection != 0) DisconnectEvents(); m_pTool->m_bRunned = FALSE; if(m_pTool != NULL) { m_pTool->Release (); m_pTool = NULL; } if(m_pRegMets != NULL) { m_pRegMets->Release(); m_pRegMets = NULL; } CPreviewWnd *pPreview = (CPreviewWnd *)GetDlgItem(IDC_PREVIEW); pPreview->ClearAll(); } CDialog::OnSysCommand(nID, lParam ); } ///------------------------------------------ // DragOutline functions BOOL CSdiDialog::BeginDrag(const CPoint& point) { VariantClear(&m_dragOutline); SAFEARRAYBOUND bound; bound.cElements = 0; bound.lLbound = 0; m_dragOutline.vt = VT_ARRAY|VT_R8; m_dragOutline.parray = ::SafeArrayCreate(VT_R8, 1, &bound); BoundingBox* Box = NULL; IVertex* vMax = NULL; IVertex* vMin = NULL; double x, y, x1, y1; x = x1 = y = y1 = 0; try { HRESULT hRes = m_pDragGraphic->CreateDragOutline(&m_dragOutline); CHECK_HRESULT(hRes) m_bDragging = TRUE; m_pDragGraphic->put_Visible(FALSE); VARIANT varState; varState.vt = VT_I4; varState.lVal = imsiDragBegin; hRes = m_pDragGraphic->CalcBoundingBox(NULL, &Box); CHECK_HRESULT(hRes) hRes = Box->get_Max(&vMax); CHECK_HRESULT(hRes) hRes = Box->get_Min(&vMin); CHECK_HRESULT(hRes) hRes = vMax->get_X(&x); CHECK_HRESULT(hRes) hRes = vMax->get_Y(&y); CHECK_HRESULT(hRes) hRes = vMin->get_X(&x1); CHECK_HRESULT(hRes) hRes = vMin->get_Y(&y1); CHECK_HRESULT(hRes) x = x1 + (x - x1)/2; y = y1 + (y - y1)/2; hRes = m_pTCADView->ViewToScreen(x, y, &x1, &y1, NULL); CHECK_HRESULT(hRes) hRes = m_pTCADView->DragOutline(&m_dragOutline, x1, y1, &varState); CHECK_HRESULT(hRes) varState.lVal = imsiDragContinue; hRes = m_pTCADView->DragOutline(&m_dragOutline, point.x, point.y, &varState); CHECK_HRESULT(hRes) } catch (...) { TRACE_EXCEPTION("CSdiDialog::BeginDrag") } if (vMin != NULL) { vMin->Release(); vMin = NULL; } if (vMax != NULL) { vMax->Release(); vMax = NULL; } if (Box != NULL) { Box->Release(); Box = NULL; } return TRUE; } void CSdiDialog::Drag(const CPoint& point) { if (m_pTCADView == NULL) return; VARIANT varState; varState.vt = VT_I4; varState.lVal = imsiDragContinue; try { HRESULT hRes = m_pTCADView->DragOutline(&m_dragOutline, point.x, point.y, &varState); CHECK_HRESULT(hRes) } catch (...) { TRACE_EXCEPTION("CSdiDialog::Drag") } } void CSdiDialog::EndDrag(const CPoint& point) { m_bDragging = FALSE; if (m_pTCADView == NULL || m_pDragGraphic == NULL) return; VARIANT varState; varState.vt = VT_I4; varState.lVal = imsiDragEnd; IMatrix *pMat = NULL; try { HRESULT hRes = m_pTCADView->DragOutline(&m_dragOutline, point.x, point.y, &varState); CHECK_HRESULT(hRes) VariantClear(&m_dragOutline); double dx, dy, dz; dx = dy = dz = 0; double dxWorld, dyWorld, dzWorld; dxWorld = dyWorld = dzWorld = 0; hRes = m_pTCADView->ScreenToView(point.x, point.y, &dx, &dy); CHECK_HRESULT(hRes) VARIANT var; var.vt = VT_ERROR; var.scode = DISP_E_PARAMNOTFOUND; ImsiSpaceModeType curMode; hRes = m_pTCADView->get_SpaceMode(&curMode); CHECK_HRESULT(hRes) dxWorld = dx; dyWorld = dy; if(curMode == imsiModelSpace) { hRes = m_pTCADView->ViewToWorld(dx, dy, dz, &dxWorld, &dyWorld, &dzWorld); CHECK_HRESULT(hRes) } hRes = m_pDragGraphic->MoveAbsolute(dxWorld, dyWorld, 0, &var, &var, &var, &pMat); CHECK_HRESULT(hRes) m_pDragGraphic->put_Visible(TRUE); } catch (...) { TRACE_EXCEPTION("CSdiDialog::EndDrag") } if(pMat != NULL) { pMat->Release(); pMat = NULL; } if (m_pDragGraphic != NULL) { m_pDragGraphic->Release(); m_pDragGraphic = NULL; } } void CSdiDialog::RestartDrag(const CPoint& point) { try { m_bDragging = FALSE; VARIANT varState; varState.vt = VT_I4; varState.lVal = imsiDragEnd; HRESULT hRes = m_pTCADView->DragOutline(&m_dragOutline, point.x, point.y, &varState); CHECK_HRESULT(hRes) VariantClear(&m_dragOutline); BeginDrag(point); } catch (...) { TRACE_EXCEPTION("CSdiDialog::RestartDrag") } } void CSdiDialog::DrawingBeforeClose(LPDISPATCH WhichDrawing, BOOL FAR* Cancel) { *Cancel = FALSE; Drawings *pDrs = NULL; long n = 0; try { HRESULT hRes = m_pIApplication->get_Drawings(&pDrs); CHECK_HRESULT(hRes) hRes = pDrs->get_Count(&n); CHECK_HRESULT(hRes) if (n == 2) { if(m_dwEventConnection != 0) DisconnectEvents(); m_pTool->m_bRunned = FALSE; CPreviewWnd *pView = (CPreviewWnd *)GetDlgItem(IDC_PREVIEW); pView->ClearAll(); CSdiDialog::OnClose(); } } catch (...) { TRACE_EXCEPTION("CSdiDialog::DrawingBeforeClose") } if (pDrs != NULL) { pDrs->Release(); pDrs = NULL; } } void CSdiDialog::OnInsertsymb() { HRESULT hRes = E_FAIL; IGraphic *pIGr = NULL; IDrawing *pActDr = NULL; Graphics *pGrs = NULL; COleVariant varItem = (1L); COleVariant varOptional(varMissing); View *pView = NULL; try { CPreviewWnd *pPreview = (CPreviewWnd *) (GetDlgItem(IDC_PREVIEW)); if (m_pIApplication) { hRes = m_pIApplication->get_ActiveDrawing(&pActDr); if (SUCCEEDED(hRes) && pActDr) { hRes = pActDr->get_Graphics(&pGrs); if (SUCCEEDED(hRes) && pGrs) { if (pPreview->m_pPreviewGraphic) { hRes = pPreview->m_pPreviewGraphic->Duplicate(&pIGr); CHECK_HRESULT(hRes) RELEASE(pIGr); hRes = pPreview->m_pPreviewGrs->Remove(&varItem, &pIGr); CHECK_HRESULT(hRes) hRes = pIGr->put_ID(0); // CHECK_HRESULT(hRes) hRes = pGrs->AddGraphic(pIGr, varOptional, varOptional); CHECK_HRESULT(hRes) hRes = pIGr->Update(); //CHECK_HRESULT(hRes) RELEASE(pIGr); hRes = pActDr->get_ActiveView(&pView); if (SUCCEEDED(hRes) && pView) { hRes = pView->Refresh(); RELEASE(pView) } } RELEASE(pGrs) } RELEASE(pActDr) } } } catch(...) { TRACE("void CSdiDialog::OnInsertsymb() "); } }